package com.example.rtmppush;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

public class ScreenPushService extends Service {

    private String NOTIFICATION_CHANNEL_ID = "ScreenPushService_ID";
    H264Encoder h264Encoder;
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        createNotification();
    }

    void createNotification() {
        Log.e("createNotification","创建通知");
        // o api 26 8.0
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Intent notificationIntent = new Intent(this,ScreenPushService.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);
            Notification notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_round))
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle("录屏中")
                    .setContentText("屏幕正在录制中.....")
                    .setTicker("SCREEN_PUSHAPP")
                    .setContentIntent(pendingIntent).build();
            NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,"SCREEN_PUSHAService", NotificationManager.IMPORTANCE_DEFAULT);
            channel.setDescription("this is screen record");
            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(channel);
            startForeground(10000,notification);
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

       if (intent!=null) {
            int resultCode = intent.getIntExtra("resultCode",-1);
            Intent data = intent.getParcelableExtra("data");

            MediaProjectionManager mediaProjectionManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
            MediaProjection mediaProjection = mediaProjectionManager.getMediaProjection(resultCode, data);
            ScreenLive screenLive = new ScreenLive();
//            screenLive.setSavePath(this.getExternalFilesDir("screenPush").getAbsolutePath());
            screenLive.startLive("rtmp://live-push.bilivideo.com/live-bvc/?streamname=live_299750326_71525520&key=5a1885393decb1023be8f2948cb4a3cf&schedule=rtmp&pflag=1",mediaProjection);
            screenLive.start();
        }
        //        h264Encoder = new H264Encoder(mediaProjection,this.getExternalFilesDir("screenPush").getAbsolutePath());
//        h264Encoder.start();
        return super.onStartCommand(intent, flags, startId);
    }
}
